﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_basisset_Group : AdminPageBase
{
    private string SearchWord;
    private string returnURL;
    private int SearchFlag;

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        if (!IsPostBack)
        {
            // 正确的属性设置方法
            this.grdvGroup.Attributes.Add("SortExpression", "GroupId");
            this.grdvGroup.Attributes.Add("SortDirection", "ASC");
          
             BindGroup();

           
        }
    }

    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
    }
    protected void BindGroup()
    {

        // 获取GridView排序数据列及排序方向
        string sortExpression = this.grdvGroup.Attributes["SortExpression"];
        string sortDirection = this.grdvGroup.Attributes["SortDirection"];


        DataSet ds = jtDataAccess.GetList("[Group]", "1=1", "Name");
        if (CoreTools.CheckDataSet(ds))
        {
            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }

            // GridView绑定并显示数据
            GridviewControl.GridViewDataBind(this.grdvGroup, ds.Tables[0]);

        }
        else
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("GroupId");
            dt.Columns.Add("GroupName");
            dt.Columns.Add("Remark");
            dt.Columns.Add("");
            GridviewControl.GridViewDataBind(this.grdvGroup, dt);

        }
    
      
    }


    protected void pager1_PageChanged(object sender, EventArgs e)
    {
        BindGroup();
    }

   
    protected void grdvGroup_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string name, remark;
        int GroupId;
        int flag = 0;
        if (e.CommandName == "Edit")
        {
            int fg = 0;
            if (grdvGroup.Rows.Count > 0)
                fg = 1;
            returnURL = "Group.aspx?MenuId=" + MenuId;
            Response.Redirect("GroupDetail.aspx?id=" + int.Parse(e.CommandArgument.ToString()) + "&returnURL=" + Server.UrlEncode(returnURL));
        }
        else if (e.CommandName == "Delete")
        {
            GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
            name = ((Label)drv.FindControl("lblname")).Text.Trim();
            remark = ((Label)drv.FindControl("lblremark")).Text.Trim();
            GroupId = Convert.ToInt32(e.CommandArgument.ToString());
           
            flag = jtDataAccess.OpGroup(JTDataAccess.OpType.Delete,GroupId,remark,name);

            switch (flag)
            {

                case -2:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("已经有用户，不允许删除！"), false);
                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除失败！"), false);
                    break;
                default:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除成功！"), false);
                    BindGroup();
                    break;
            }

        }

    }
    protected void grdvGroup_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
           
            Label lblname = (Label)e.Row.FindControl("lblname");
            LinkButton lbtDelete = (LinkButton)e.Row.FindControl("lbtDelete");
            if (lblname != null && lblname.Text == "Administrators")
            {
                lbtDelete.Visible = false;
            }

        }
    }
   
    protected void grdvGroup_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        grdvGroup.EditIndex = -1;
        BindGroup();
    }
   

    protected void grdvGroup_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.grdvGroup.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.grdvGroup.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.grdvGroup.Attributes["SortExpression"] = sortExpression;
        this.grdvGroup.Attributes["SortDirection"] = sortDirection;

        BindGroup();
    }




    protected void addGroup_Click(object sender, EventArgs e)
    {
        int flag = 0;
        if (grdvGroup.Rows.Count > 0)
            flag = 1;
        returnURL = "Group.aspx?MenuId=" + MenuId;
        Response.Redirect("GroupDetail.aspx?returnURL=" + Server.UrlEncode(returnURL));
    }
}